/*
 * Copyright 2011 Noctarius (Christoph Engelbert)
 * Part of www.sourceprojects.org 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); you may 
 * not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *    http://www.apache.org/licenses/LICENSE-2.0 
 *
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 */
package org.sourceprojects.cappadocia;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.Attribute;
import org.objectweb.asm.ClassVisitor;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;

class ClassInfoParser implements ClassVisitor {
	private final ClassPool classPool;
	private final ClassInfoImpl classInfo;

	ClassInfoParser(final ClassPool classPool, final ClassInfoImpl classInfo) {
		this.classPool = classPool;
		this.classInfo = classInfo;
	}

	@Override
	public void visit(final int version, final int access, final String name,
			final String signature, final String superName,
			final String[] interfaces) {

		final String canonicalName = name.replace("/", ".").replace("\\", ".");

		classInfo.setModifiers(access);
		classInfo.setCanonicalName(canonicalName);
		classInfo.setName(canonicalName.substring(canonicalName
				.lastIndexOf(".") + 1));

		for (final String interfaze : interfaces) {
			classInfo.getInterfaces0().add(
					interfaze.replace("/", ".").replace("\\", "."));
		}

		classInfo.setSuperclass(superName != null ? superName.replace("/", ".")
				.replace("\\", ".") : null);
	}

	@Override
	public AnnotationVisitor visitAnnotation(final String desc,
			final boolean visible) {

		return new AnnotationInfoParser(desc, classInfo);
	}

	@Override
	public void visitAttribute(final Attribute attr) {
	}

	@Override
	public void visitEnd() {
	}

	@Override
	public FieldVisitor visitField(final int access, final String name,
			final String desc, final String signature, final Object value) {

		return null;
	}

	@Override
	public void visitInnerClass(final String name, final String outerName,
			final String innerName, final int access) {

	}

	@Override
	public MethodVisitor visitMethod(final int access, final String name,
			final String desc, final String signature, final String[] exceptions) {

		final MethodInfoImpl impl = new MethodInfoImpl(classPool, name,
				signature, access);

		return new MethodInfoParser(classInfo, impl);
	}

	@Override
	public void visitOuterClass(final String owner, final String name,
			final String desc) {
	}

	@Override
	public void visitSource(final String source, final String debug) {
	}

}
